function [A,V,inputSize]=generateSimpleConnection(x)
%GENERATESIMPLECONNECTION  A function used by ODN project.
%   used to generate a simple connected network for input points
%   export an Aii with 1 for linked routes and 0 for unlinked routes.
%   export an Vi2 including the points necessary for plotting.
%   IMPORTANT: i should be >= the original length of x.
%   August 3 2014  Risoe Denmark. Qin Wang.
%   Updated Nov 4 2014  Tsinghua Beijing. Qin Wang.
%       optimize the calculation process, so it can now quickly calculate
%       input numbers bigger than 1000.
%       Basic idea: choose the closest point as a1, and calculate a2.

if nargin==1
    sizetmp=size(x);
    if sizetmp(2)==2
        inputSize=sizetmp(1);           %Get the number of points.
        A=zeros(inputSize);
        V=x;
        for i=2:inputSize
            if i==2
                A(1,2)=1;
                A(2,1)=1;
            else
                distanceRef=1000;       %Set a big number here
                
                %做法，先合并1：i-1+先前生成的分岔节点。然后计算最近的点，然后用那些点算最小的
                B=zeros((i-1+sizetmp(1)-inputSize),3);%临时矩阵，计算待测点到所有之前点的距离，并给出序号
                for a1=1:i-1
                    B(a1,2)=a1;
                    B(a1,1)=(V(i,1)-V(a1,1))^2+(V(i,2)-V(a1,2))^2;
                end
                for a1=inputSize+1:sizetmp(1)
                    a2=a1-inputSize-1+i;
                    B(a2,2)=a1;
                    B(a2,1)=(V(i,1)-V(a1,1))^2+(V(i,2)-V(a1,2))^2;
                end
                [xB,yB]= find(B==min(min(B(:,1))));
                a1=B(xB,2);
                
                netcount=0;
            
                    for a2=1 : i-1  
                        if  A(a1,a2)>0
                            [position,signal,distance] = getVerticalPoint(V(a1,:),V(a2,:),V(i,:));
                            if signal==1 && distance<distanceRef %signal=1:
                                positionRef=position;
                                distanceRef=distance;
                                a1Ref=a1;
                                a2Ref=a2;
                                netcount=netcount+1;
                            end
                        end
                    end

                    for a2=inputSize+1:sizetmp(1)
                        if  A(a1,a2)>0
                            [position,signal,distance] = getVerticalPoint(V(a1,:),V(a2,:),V(i,:));
                            if signal==1 && distance<distanceRef 
                                positionRef=position;
                                distanceRef=distance;
                                a1Ref=a1;
                                a2Ref=a2;
                                netcount=netcount+1;
                            end
                        end
                    end
                
                if netcount==0
                    A(i,a1)=1;
                    A(a1,i)=1;
                else
                    V=[V;positionRef];
                    sizetmp=size(V);
                    A(i,sizetmp(1))=1;
                    A(sizetmp(1),i)=1;
                    A(a1Ref,sizetmp(1))=1;
                    A(sizetmp(1),a1Ref)=1;
                    A(a2Ref,sizetmp(1))=1;
                    A(sizetmp(1),a2Ref)=1;
                    %plotSimpleNetwork( A,V,inputSize)
                end                
            end
        end
    else
        error('Wrong inputs formats.')
    end

else 
    error('Missing/Too many inputs.')
end
